const Items = require('../model/items.model');

// 使用模糊查询需要先引入Op
const seq = require('sequelize');
const Op = seq.Op;

// 服务层 读写数据库
class ItemsService {
    // 添加物品
    async createItems(itemId, itemName, itemDesc, price, itemType, pileNumber, itemPath, relationId, relationType, foodHP, foodEP, foodBuff, composition) {
        // Items.create({
        //     itemName: itemName,
        //     itemDesc: itemDesc
        // });
        // 属性和值一样可以简写
        let res = await Items.create({
            itemId,
            itemName,
            itemDesc,
            price,
            itemType,
            pileNumber,
            itemPath,
            relationId,
            relationType,
            composition, foodHP, foodEP, foodBuff,
        });
        return res;
    }

    async getItemsInfo({id, itemName, itemType, itemDesc, flag, pageInfo}) {
        let whereObj = {};
        id && Object.assign(whereObj, {id});
        itemDesc && Object.assign(whereObj, {itemDesc});
        itemType && Object.assign(whereObj, {itemType});
        flag && Object.assign(whereObj, {flag});

        itemName && Object.assign(whereObj, {
            itemName: {
                [Op.like]: `%${itemName}%`
            },
        });
        // 查数量
        let count = await Items.count({where: whereObj});
        let res;
        if (count !== 0) {
            let _pageInfo = {};
            if (pageInfo) {
                let {pageIndex, pageSize} = JSON.parse(pageInfo);
                _pageInfo = {
                    limit: pageSize,
                    offset: (pageIndex - 1) * pageSize
                }
            }
            res = await Items.findAll({
                where: whereObj,
                ..._pageInfo
            });
        }
        return {list: res, count: count} || null;
    }

    async updateItemsInfo({id, itemId, itemName, itemDesc, price, itemType, pileNumber, itemPath, relationId, relationType, foodHP, foodEP, foodBuff, composition, flag}) {

        let whereObj = {id};
        let newItems = {};
        itemId && Object.assign(newItems, {itemId});
        itemName && Object.assign(newItems, {itemName});
        itemDesc && Object.assign(newItems, {itemDesc});
        price && Object.assign(newItems, {price});
        itemType && Object.assign(newItems, {itemType});
        pileNumber && Object.assign(newItems, {pileNumber});
        itemPath && Object.assign(newItems, {itemPath});
        relationId && Object.assign(newItems, {relationId});
        relationType && Object.assign(newItems, {relationType});
        foodHP && Object.assign(newItems, {foodHP});
        foodEP && Object.assign(newItems, {foodEP});
        foodBuff && Object.assign(newItems, {foodBuff});
        composition && Object.assign(newItems, {composition});
        flag && Object.assign(newItems, {flag});
        let res = await Items.update(newItems, {
            where: whereObj
        });
        return res[0] > 0;
    }
}

module.exports = new ItemsService();